import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// import store from '@/store'
import { isLogin } from '@/utils/user'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: () => import(/* webpackChunkName: "Home" */ '../views/Home.vue')
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue')
    },
    {
        path: '/user',
        name: 'User',
        component: () => import(/* webpackChunkName: "User" */ '../views/User.vue'),
        meta: {
            auth: true
        }
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach(async (to, from, next) => {
    if (to.meta.auth) {
        if (!isLogin()) {
            next('/login')
        } else {
            next()
        }
        return
    }
    next()
})

export default router
